package com.common.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.common.user.domain.Role;
import com.common.user.repository.RoleRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * Role api
 * @author enbool
 *
 */
@Api(description = "Manage Role")
@RestController
@RequestMapping(value = "/roles")
public class RoleController {
	
	@Autowired
	RoleRepository roleRepository;
	
	@ApiOperation(value = "Create a role")
	@PostMapping
	public Role create(@RequestBody Role role) {
		
		return roleRepository.save(role); 
	}
	
	@ApiOperation(value = "Delete a role")
	@ApiImplicitParam(name = "id", value = "The id of the role to delete.")
	@DeleteMapping(value = "/{id:\\d+}")
	public void delete(@PathVariable Long id) {
		
		roleRepository.delete(id);
	}
	
	@ApiOperation(value = "Update a role")
	@PutMapping
	public Role update(@RequestBody Role role) {
		
		return roleRepository.save(role);
	}
	
	@ApiOperation(value = "List of roles")
	@GetMapping
	public List<Role> findAll() {
		
		return roleRepository.findAll();
	}
	
	@ApiOperation(value = "Get a role")
	@ApiImplicitParam(name = "id", value = "The id of the role to get.")
	@GetMapping(value = "/{id:\\d+}")
	public Role findOne(@PathVariable Long id) {
		
		return roleRepository.getOne(id);
	}
}
